package com.eqxiu.ieditor.commons.web;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回前端的数据包装
 * 
 * @author weiming
 * 
 * @param
 */
@SuppressWarnings("rawtypes")
public class ResultData implements Serializable {

	// protected Logger logger = LoggerFactory.getLogger(getClass());
	/*
	 * 调用结果
	 */
	private boolean success;

	/*
	 * 错误或成功代码
	 */
	private int code;

	/*
	 * 服务器端返回消息
	 */
	private String msg;

	private String language;

	/*
	 * 数据对象
	 */
	private Object obj;

	private Map<String, Object> map;

	/*
	 * 集合数据对象
	 */
	private List list;

	public ResultData() {
	}

	public boolean success() {
		return success;
	}

	public int getCode() {
		return code;
	}

	public ResultData setCode(int code) {
		this.code = code;
		return this;
	}

	public boolean isSuccess() {
		return success;
	}

	public ResultData setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	/**
	 * 分解code返回提示信息 未设置code时，如果有msg信息直接返回msg，否则默认返回500错误并打印日志提示开发者
	 */
	public String getMsg() {
		return msg;
	}

	public ResultData setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public Object getObj() {
		return obj;
	}

	public ResultData setObj(Object data) {
		this.obj = data;
		return this;
	}

	public List getList() {
		return list;
	}

	public ResultData setList(List list) {
		this.list = list;
		return this;
	}

	public Map getMap() {
		return map;
	}

	public ResultData setMap(Map<String, Object> map) {
		this.map = map;
		return this;
	}

	public ResultData addAttribute(String key, Object value) {
		if (this.map != null) {
			this.map.put(key, value);
		} else {
			this.map = new HashMap<String, Object>();
			this.map.put(key, value);
		}
		return this;
	}

	// public ResultData setPage(Page page){
	// this.list = page.getList();
	// if(this.map == null){
	// this.map = new HashMap<String,Object>();
	// }
	// this.map.put("pageNo", page.getPageNo());
	// this.map.put("pageSize", page.getPageSize());
	// this.map.put("count", page.getCount());;
	// return this;
	// }

	public static ResultData result(boolean result) {
		return new ResultData().setSuccess(result).setCode(result ? MsgCode.OK : MsgCode.SERVER_ERROR);
	}

}
